#include "PartitionElement.h"
#include "Sudoku.h"

using namespace std ;


PartitionElement::PartitionElement(Cell *newCell, short valueNew )
    :type(Value),cell(newCell),
      oldValue(cell->getValue()), newValue(valueNew)

{}
PartitionElement::PartitionElement(Cell *newCell,
                                   short assumptionValue, bool canState )
    :type(Hypothesis), cell(newCell),
      assumValue(assumptionValue), digitCanState(canState)
{

}

ostream& operator<<( std::ostream& out, PartitionElement element )
{
    if( Value == element.type )
        out <<"NewValue=" << element.newValue << " OldValue=" << element.oldValue
           << " (" << element.transactionID << ")" ;
    else
        out << "AssumVal=" << element.assumValue
            << " CanState=" << element.digitCanState
            << " (" << element.transactionID << ")" ;
    return out ;
}
/*
SmallBlock* PartitionElement::getSmallBlock()
{
    return smallBlock ;
}
*/

short PartitionElement::getNewValue()
{
    return newValue ;
}
short PartitionElement::getOldValue()
{
    return oldValue ;
}


void PartitionElement::setOldValue( short newOldValue )
{
    oldValue = newOldValue ;
}

bool PartitionElement::exec()
{



    if( Value == type )
    {

        cell->setValue( newValue );
        return true ;
    }
    else
    {
        if( digitCanState )
        {
            return cell->addAssum( assumValue ) ;
        }
        else
        {
            return cell->delAssum( assumValue ) ;

        }

    }
}
void PartitionElement::revert()
{
    //cout << "Canceled change, type=" << type << endl ;
    if( Value == type )
    {
        //cout << "OldValue=" << cell->getValue() ;
        cell->setValue( oldValue );
       // cout << " NewValue=" << cell->getValue() << endl ;
       // system( "pause" ) ;
    }
    else
    {
        if( digitCanState )
            cell->delAssum( assumValue ) ;
        else
            cell->addAssum( assumValue ) ;
    }
}

void PartitionElement::radicallyChange()
{
    if( Value == type )
    {
        type = Hypothesis ;
        assumValue = newValue ;
    }
}

int PartitionElement::getTransactionID()
{
    return transactionID ;
}

void PartitionElement::setTransactionID( int newTransactionID )
{
    transactionID = newTransactionID ;
}

Cell* PartitionElement::getCell()
{ return cell ; }

